﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;

public partial class VideoList : SitePage
{
  
    protected override void OnInit(EventArgs e)
    {
        InitData();
        base.OnInit(e);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadData();
    }
    protected void dalVideo_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        DataListItem item = e.Item;
        Video data = new Video();
        if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
        {
            if (data == null)
                return;
            data = (Video)item.DataItem;
            PlaceHolder phdControl = (PlaceHolder)item.FindControl("phdControlVideo");
            UserControl ctrl = new UserControl();
            UserControl_Video uc = (UserControl_Video)ctrl.LoadControl("~/UserControl/Video.ascx");
            if (uc != null)
            {
                uc.objVideo = data;
            }
            phdControl.Controls.Add(uc);
        }
    }
    protected void pagerTop_PageIndexChanging(object obj, CommonTagTime.PagerControl.PageIndexChangeEventArgs e)
    {
        pagerBottom.PageIndex = pagerTop.PageIndex = e.PageIndex;
        LoadData();
    }
    protected void pagerTop_PageSizeChanging(object obj, CommonTagTime.PagerControl.PageSizeChangeEventArgs e)
    {
        pagerBottom.PageIndex = pagerTop.PageIndex = 1;
        pagerBottom.PageSize = pagerTop.PageSize = e.PageSize;
        LoadData();
    }
    private void LoadData()
    {
        string conditionCate = string.Empty;
        int cate = clsQueryString.GetInt("cateId");
        if (cate > 0)
        {
            busVideoCategory objBusCate = new busVideoCategory();
            VideoCategoryCollection lstCate = objBusCate.GetByCategoryId(cate);
            if (lstCate != null && lstCate.Count > 0)
            {
                foreach (VideoCategory objCate in lstCate)
                {
                    conditionCate += objCate.VideoID + ",";
                }
            }
            LoadMetaData(cate);
        }
        if (conditionCate != string.Empty)
        {
            conditionCate = conditionCate.Substring(0, conditionCate.Length - 1);
        }
        VideoCollection lstResult = new VideoCollection();
        busVideo objBus = new busVideo();
        //get Condition
        Video objVideo = new Video();
        objVideo.CurrentPage = pagerTop.PageIndex;
        objVideo.OrderBy = busVideo.SORTORDER;
        objVideo.OrderDirection = "ASC";
        objVideo.PageSize = pagerTop.PageSize;
        objVideo.Condition = " v.IsActive=1 ";
        if (cate > 0)
        {
            if(conditionCate!=string.Empty)
                objVideo.Condition = objVideo.Condition + " and v.VideoID in(" + conditionCate + ") ";
            else
                objVideo.Condition = objVideo.Condition + " and v.VideoID<0";
        }
        lstResult = objBus.List(objVideo);
        int count = 0;
        if (lstResult != null && lstResult.Count > 0)
        {
            count = lstResult.Count;
            dalVideo.DataSource = lstResult;
            dalVideo.DataBind();
            pagerTop.SetPaging(count, objVideo.TotalRecords);
            pagerBottom.SetPaging(count, objVideo.TotalRecords);
            pagerTop.Visible = true;
            pagerBottom.Visible = true;
        }
        else
        {
            pagerTop.Visible = false;
            pagerBottom.Visible = false;
        }

    }

    private void LoadMetaData(int cateId)
    {
        busCategory objBusCate = new busCategory();
        Category objCate = objBusCate.GetByID(cateId);
        string title = string.Empty;
        if (objCate != null)
        {
            if (objCate.PageTitle == string.Empty)
                title = objCate.Name;
            else title = objCate.PageTitle;
            this.SetMetaData(string.Format(Resources.Resource.TITLE, Server.HtmlEncode(title)), objCate.MetaDescription, objCate.MetaKeywords);
        }

    }
    private void InitData()
    {
        this.pagerTop.PageIndex = pagerBottom.PageIndex = 1;
        this.pagerTop.PageSize = this.pagerBottom.PageSize = clsConfigData.DEFAULT_VIDEO_PAGE_SIZE;     
        this.Page.Header.Title = string.Format(Resources.Resource.TITLE, "Video");
    }
}